module prox_detect(
	input clk,
	input rst_n,
	
	output i2c_scl,
	inout i2c_sda,
	output seg_rck,
	output seg_sck,
	output seg_din,

	output [7:0]led,
	output [8:0]digit1,
	output [8:0]digit2,
	output buzz
);

wire dat_valid;
wire [15:0] ch0_dat, ch1_dat, prox_dat;
wire [31:0] lux_data;
wire [2:0] dist;

reg buzz_ctrl;

rpr0521rs_driver u1(
	.clk (clk),
	.rst_n(rst_n),
	
	.i2c_scl(i2c_scl),
	.i2c_sda(i2c_sda),
	
	.dat_valid(dat_valid),
	.ch0_dat(ch0_dat),
	.ch1_dat(ch1_dat),
	.prox_dat(prox_dat)
);
 
decoder u2(
	.rst_n(rst_n),
	
	.dat_valid(dat_valid),
	.ch0_dat(ch0_dat),
	.ch1_dat(ch1_dat),
	.prox_dat(prox_dat),
	
	.lux_data(lux_data),
	//.Y_out(led),
	.raw_dist(dist)
);

segment_scan u3(
	.clk(clk),
	.rst_n(rst_n),
	.dat_1(lux_data[31:28]),
	.dat_2(lux_data[27:24]),
	.dat_3(lux_data[23:20]),
	.dat_4(lux_data[19:16]),
	.dat_5(lux_data[15:12]),
	.dat_6(lux_data[11:08]),
	.dat_7(lux_data[07:04]),
	.dat_8(lux_data[03:00]),
	.dat_en(8'b1111_1111),
	.dot_en(8'b0000_0100),
	.seg_rck(seg_rck),
	.seg_sck(seg_sck),
	.seg_din(seg_din)
);

HexSegDecoder u4 (
	.ctrl({1'b0, dist}),
	.dot_in(1'b0),
	.seg(digit1[6:0]),
	.dot(digit1[7]),
	.gnd(digit1[8])
);

BuzzControl#(
	.width(36),
	.buzzDiv1(5000),
	.buzzDiv2(12_000_000)
) u5(
	.clk(clk),
	.rst_n(rst_n),
	.ctrl(buzz_ctrl),
	
	.buzz(buzz)
);

always @(posedge clk) begin
	if(dist > 3'd5) begin
		buzz_ctrl <= 1'b1;
	end
	else begin
		buzz_ctrl <= 1'b0;
	end
end

reg led_reg;
always @(posedge clk) begin
	if(
		lux_data[31:28] < 4'd1 &&
		lux_data[27:24] < 4'd1 &&
		lux_data[23:20] < 4'd1 && 
		lux_data[19:16] < 4'd1
	) begin
		led_reg <= 1'b0;
	end
	else begin
		led_reg <= 1'b1;
	end
end

assign led[0] = led_reg;
assign led[1] = led_reg;
assign led[2] = led_reg;
assign led[3] = led_reg;
assign led[4] = led_reg;
assign led[5] = led_reg;
assign led[6] = led_reg;
assign led[7] = led_reg;


endmodule
